# -*- coding: utf-8 -*-
import hashlib
from model import Collection, db


class Teacher(Collection):
    '''
* `_id` (ObjectId) - ID of each user
* `answerList` ([string]) - answer list of user
* `askMeList` ([string]) - ask me list of teacher
* `avatarUrl` (string) - Url of avatar
* `email` (string) - email of user
* `collection` ([string]) - collection of user
* `fan` ([string]) - fan of user
* `follow` ([string]) - people followed by user
* `gender` (int) - 0 means female, 1 means male, 2 means undetermined
* `name` (string) - user name
* `password` (string) - password of user
* `phone` (int) - phone of user
* `school` (string) - school of user
* `subscription` ([string]) - subscription of user
* `workList` ([string]) - work list of user
* `place` (dict) - place of user
    * `province` (string) - province of user
    * `city` (string) - city of user
* `register` (int) - 1 if user is verified
* `weibo` (dict) - user info from weibo
    * `pId` (string) - ID of weibo
    * `pToken` (string) - Token of weibo
    '''

    collection = db.teacher

    class Field(object):
        _id = '_id'
        answerList = 'answerList'
        askMeList = 'askMeList'
        avatarUrl = 'avatarUrl'
        collection = 'collection'
        email = 'email'
        fan = 'fan'
        follow = 'follow'
        gender = 'gender'
        name = 'name'
        password = 'password'
        phone = 'phone'
        school = 'school'
        subscription = 'subscription'
        workList = 'workList'
        place = 'place'
        register = 'register'

    class Gender(object):
        female = 0
        male = 1
        other = 2

    class Place(object):
        city = 'city'
        province = 'province'

    class Register(object):
        unverified = 0
        verified = 1

    class Weibo(object):
        pId = 'pId'
        pToken = 'pToken'

    class Rank(object):
        pass

    class Wechat(object):
        pass

    collection.ensure_index(Field.email, sparse=True, unique=True)
    collection.ensure_index(Field.phone, sparse=True, unique=True)

    @staticmethod
    def new_teacher(email, password):
        md5 = hashlib.md5()
        md5.update(password)
        hash_password = md5.hexdigest()
        return Teacher.insert({
            Teacher.Field.answerList: [],
            Teacher.Field.askMeList: [],
            Teacher.Field.avatarUrl: '',
            Teacher.Field.collection: [],
            Teacher.Field.email: email,
            Teacher.Field.fan: [],
            Teacher.Field.follow: [],
            Teacher.Field.gender: Teacher.Gender.other,
            Teacher.Field.name: '',
            Teacher.Field.password: hash_password,
            Teacher.Field.school: '',
            Teacher.Field.subscription: [],
            Teacher.Field.workList: [],
            Teacher.Field.place: {
                Teacher.Place.city: '',
                Teacher.Place.province: '',
            },
            Teacher.Field.register: Teacher.Register.verified
        })
